package abcy.cloud.syscoreserver.services.impls;

import abcy.cloud.blogcommon.builder.TypeBuilder;
import abcy.cloud.blogcommon.enums.SignEnum;
import abcy.cloud.blogcommon.utils.CodeUtil;
import abcy.cloud.blogcommon.utils.UUIDUtil;
import abcy.cloud.syscoreserver.dto.req.SysRoleAddReqDto;
import abcy.cloud.syscoreserver.dto.req.SysRoleSelectReqDto;
import abcy.cloud.syscoreserver.dto.resp.SysRoleSelectRespDto;
import abcy.cloud.syscoreserver.entities.SysRole;
import abcy.cloud.syscoreserver.entities.SysRoleMenu;
import abcy.cloud.syscoreserver.entities.SysRolePermission;
import abcy.cloud.syscoreserver.mappers.SysRoleMapper;
import abcy.cloud.syscoreserver.mappers.SysRoleMenuMapper;
import abcy.cloud.syscoreserver.mappers.SysRolePermissionMapper;
import abcy.cloud.syscoreserver.services.SysRoleService;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Autowired
    private SysRolePermissionMapper sysRolePermissionMapper;
    @Override
    public PageInfo<SysRoleSelectRespDto> selectRolesForPage(SysRoleSelectReqDto sysRoleSelectReqDto) {
        PageHelper.startPage(sysRoleSelectReqDto.getPageIndex(), sysRoleSelectReqDto.getPageSize());
        List<SysRoleSelectRespDto> roleLists = sysRoleMapper.getRolesForPage(sysRoleSelectReqDto);
        PageInfo<SysRoleSelectRespDto> rolePage = new PageInfo<>(roleLists);
        return rolePage;
    }

    @Override
    public void addRole(SysRoleAddReqDto sysRoleAddReqDto) {
        Date date = new Date();
        String roleId = UUIDUtil.UUID();
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        sysRoleMapper.insert(TypeBuilder.of(SysRole::new).with(SysRole::setId, roleId)
                .with(SysRole::setSignature, sysRoleAddReqDto.getSignature())
                .with(SysRole::setName, sysRoleAddReqDto.getName())
                .with(SysRole::setCode, CodeUtil.codeGenerator(SignEnum.ROLE.getValue(), sysRoleMapper.selectMaxRoleCode()))
                .with(SysRole::setIsDel, sysRoleAddReqDto.getIsDel())
                .with(SysRole::setCreateTime, date)
                .with(SysRole::setUpdateTime, date)
                .with(SysRole::setCreateBy, sysRoleAddReqDto.getCreateBy())
                .with(SysRole::setUpdateBy, sysRoleAddReqDto.getUpdateBy()).build());
        if(StringUtils.isNotEmpty(sysRoleAddReqDto.getMenus())) {
            if(CollectionUtils.isNotEmpty(Arrays.asList(sysRoleAddReqDto.getMenus().split(",")))) {
                for (String menu : Arrays.asList(sysRoleAddReqDto.getMenus().split(","))) {
                    SysRoleMenu sysRoleMenu = new SysRoleMenu();
                    sysRoleMenu.setRoleId(roleId);
                    sysRoleMenu.setId(UUIDUtil.UUID());
                    sysRoleMenu.setMenuId(menu);
                    sysRoleMenu.setCreateTime(date);
                    sysRoleMenuMapper.insert(sysRoleMenu);
                }
            }
        }
        if(Objects.nonNull(sysRoleAddReqDto.getPermissions())) {
            List<String> permissions = Arrays.asList(sysRoleAddReqDto.getPermissions().split(","));
            if(CollectionUtils.isNotEmpty(permissions)) {
                for (String permission : permissions) {
                    SysRolePermission sysRolePermission = new SysRolePermission();
                    sysRolePermission.setId(UUIDUtil.UUID());
                    sysRolePermission.setRoleId(roleId);
                    sysRolePermission.setPermissionId(permission);
                    sysRolePermission.setCreateTime(date);
                    sysRolePermissionMapper.insert(sysRolePermission);
                }
            }
        }
    }

    @Override
    public Map<String, Object> selectRolesByUserId(Map<String, Object> map) {
        String userId = map.get("userId").toString();
        List<SysRoleSelectRespDto> roleList = sysRoleMapper.selectRolesByUserId(userId);
        List<String> stringList = roleList.stream().map(e -> e.getId()).collect(Collectors.toList());
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("roleList", stringList);
        return resultMap;
    }
}
